<template>
    <div>
        <el-dialog :visible.sync="value" :show-close="false" center width="70%">
            <el-cascader-panel v-model="data.classesId" :options="computed_classes_list" :props="{emitPath:false,value:'id'}">
                <template slot-scope="{ node, data }">
                    <span>{{ data.label }}</span>
                    <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
                </template>
            </el-cascader-panel>

            <template slot="footer">
                <el-button type="primary" icon="el-icon-plus" v-loading.fullscreen.lock="fullscreenLoading" @click="handleSubmit">提交</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script>

    import { searchCasClassesList } from '@/api/principal/school-manager/school'
    import { replaceClassesByStudentId } from '@/api/quality/student-manager'
    export default {
        name: "student-replace-classes",
        data(){
            return {
                value : false,
                fullscreenLoading:false,
                parent: {},
                classesList:[],
                data: {}
            }
        },
        computed:{
            computed_classes_list(){
                let data = this.classesList;
                data.forEach(school=>{
                    if(school && school.children){
                        school.children.forEach(subSchool=>{
                            if(subSchool && subSchool.vocationalChildren){
                                subSchool.children = subSchool.vocationalChildren;
                            }
                        })
                    }
                });

                data.forEach(school=>{
                    if(school && school.children){
                        school.children.forEach(subSchool=>{
                            if(subSchool && subSchool.children){
                                subSchool.children.forEach(vocational=>{
                                    if(vocational && vocational.children){
                                        vocational.children.forEach(stage=>{
                                            if(stage && stage.children){
                                                stage.children.forEach(classes=>{
                                                    classes.label = classes.classesName;
                                                })
                                            }
                                        })
                                    }
                                })
                            }
                        })
                    }
                });

                return data;
            }
        },
        watch:{
            value(data){
                if(data){
                    this.fullscreenLoading = true;
                    searchCasClassesList().then(res=>{
                        this.classesList = res.data.data;
                        this.fullscreenLoading = false;
                    })
                }
            }
        },
        methods:{
            handleSubmit(){
                if(!this.data.classesId){
                    this.$message.error({message:'请选择班级',center:true});
                    return false;
                }

                this.$confirm("确定吗?","温馨提示",{type:'warning'}).then(()=>{
                    this.data.studentId = this.parent.id;
                    this.fullscreenLoading = true;
                    replaceClassesByStudentId(this.data).then(res=>{
                        this.value = false;
                        this.fullscreenLoading = false;
                        this.$parent.getStudentList(this.$parent.params);
                    })
                })
            }
        }
    }
</script>

<style scoped>

</style>